package com.yuanheng100.ems4j.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuanheng100.ems4j.api.IDepartmentService;
import com.yuanheng100.ems4j.api.IEmployeeService;
import com.yuanheng100.ems4j.api.IMenuService;
import com.yuanheng100.ems4j.api.IRoleService;
import com.yuanheng100.ems4j.common.reg.RegExpValidator;
import com.yuanheng100.ems4j.common.security.SecurityUtil;
import com.yuanheng100.ems4j.model.Ems4jEmployee;
import com.yuanheng100.ems4j.web.manager.SessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 员工相关请求处理类
 *
 * Created by wangguangshuo on 2016/12/15.
 * <p/>
 *
 */
@RestController
public class LoginController
{

    private Logger logger = LoggerFactory.getLogger(LoginController.class);

//    @Reference(interfaceClass = IEmployeeService.class, version = "1.0.0")
//    private IEmployeeService employeeService;
//
//    @Reference(interfaceClass = IDepartmentService.class, version = "1.0.0")
//    private IDepartmentService departmentService;
//
//    @Reference(interfaceClass = IRoleService.class, version = "1.0.0")
//    private IRoleService roleService;
//
//    @Reference(interfaceClass = IMenuService.class, version = "1.0.0")
//    private IMenuService menuService;

    @Autowired
    private IEmployeeService employeeService;


    @Autowired
    private SessionManager sessionManager;

    /**
     * 登录
     *  return int类型
     *  -1：登录信息输入错误
     *  0：登录成功
     *  1：用户不存在
     *  2：密码错误
     *
     * @return
     */
    @RequestMapping("/login")
    public Integer login(@RequestParam String username, @RequestParam String password)
    {
        logger.info(username);
        logger.info(password);
        Ems4jEmployee employee = null;
        if (RegExpValidator.isEmail(username))
        {
            employee = employeeService.getEmployeeByEmail(username);
        }
        else if (RegExpValidator.isMobile(username))
        {
            employee = employeeService.getEmployeeByMobile(Long.parseLong(username));
        }
        else
        {
            /*用户名输入有误*/
            logger.info("登录账号（"+username+"）既不是邮箱，也不是手机号");
            return -1;
        }
        if (employee == null)
        {
            /*用户不存在*/
            return 1;
        }
        logger.info(SecurityUtil.encryptMd5(password));
        if (!employee.getPassword().equals(SecurityUtil.encryptMd5(password)))
        {
            /*密码不正确*/
            return 2;
        }
        sessionManager.setEms4jEmployee(employee);
        /*登录成功*/
        return 0;
    }


    @RequestMapping("/quit")
    public void quit()
    {
        if (sessionManager.getEms4jEmployee() != null)
            logger.info("用户（"+sessionManager.getEms4jEmployee().getName()+"）退出系统");
        sessionManager.setEms4jEmployee(null);
    }




    /**
     * 添加员工
     *
     * @return
     */
//    @RequestMapping("/addEmployee")
//    public Integer addEmployee(HttpServletRequest request)
//    {
//        Employee employee = new Employee();
//        employee.setEmpName("用户1");
//        employee.setMobile(Long.parseLong("13800138000"));
//        employee.setIdCardNo("1234567891234567");
//        employee.setPassword("123456");
//        employee.setDepId(1);
//        employee.setEmail("111@qq.com");
//        employee.setEmpStatus(EmployeeStatus.WORKING);
//        employee.setWeixinGender(Gender.MALE);
//        employee.setEntryDate(new Date());
//        employee.setPosition("小职员");
//        logger.info(request.getParameter("empName"));
//        Integer empId = employeeService.addEmployee(1, employee, false);
//        logger.info("添加员工返回结果:" + empId);
//        logger.info("主键："+employee.getEmpId());
//        boolean b = roleService.addRoleToEmployee(1,empId);
//        return empId;
//    }
//
//    /**
//     * 添加部门
//     *
//     * @return
//     */
//    @RequestMapping("/addDepartment")
//    public Integer addDepartment()
//    {
//        Department department = new Department();
//        department.setDepName("测试部门");
//        department.setAddress("北京市朝阳区");
//        department.setCreateTime(new Date());
//        department.setDepType(DepartmentType.CHILDCOMPANY);
//        department.setDepStatus(UseStatus.ABLE);
//        department.setTelephone(Long.valueOf("12345678"));
//        Integer result = departmentService.addDepartment(1, department, false);
//        logger.info("返回结果：" + result);
//        return result;
//    }
//
//
//
//
//    @RequestMapping("/getOneDepartment")
//    public EMSResult getOneDepartment()
//    {
//        EMSResult result = departmentService.getDepartmentById(1, 1);
//        logger.info("返回结果：" + result);
//        return result;
//    }
//
//
//    @RequestMapping("/getDepartmentList")
//    public EMSResult getDepartmentList()
//    {
//        EMSResult result = departmentService.getDepartmentList(1);
//        logger.info("收到结果：" + result);
//        return result;
//    }
//
//    @RequestMapping("/addRole")
//    public Integer addRole()
//    {
//        Role role = new Role();
//        role.setAppId(1);
//        role.setDescription("测试角色");
//        role.setRoleName("测试员工");
//        role.setRoleStatus(UseStatus.ABLE);
//        Integer b = roleService.addRole(role, false);
//        logger.info("添加角色结果：" + b);
//        boolean result = menuService.addMenuToRole(1,b);
//        logger.info("给角色设置菜单："+result);
//        return b;
//    }
//
//
//    @RequestMapping("/addMenu")
//    public Integer addMenu()
//    {
//        Menu menu = new Menu();
//        menu.setDescription("首页");
//        menu.setAppId(1);
//        menu.setIsMenu(DingIsHide.TRUE);
//        menu.setMenuName("首页");
//        menu.setMenuStatus(UseStatus.ABLE);
//        menu.setOrderNum(1);
//        menu.setUrl("/html/index.html");
//        Integer b = menuService.addMenu(menu);
//        logger.info("添加菜单结果：" + b);
//        return b;
//    }
//
//
//
//    @RequestMapping("/getGender")
//    public EMSResult getGender()
//    {
//        EMSResult result = new EMSResult();
//        result.setObjList(Gender.genderList);
//        result.setResultCode(0);
//        result.setMsg("查询成功");
//        return result;
//    }
//
//
//    @RequestMapping("/getEmpStatus")
//    public EMSResult getEmpStatus()
//    {
//        EMSResult result = new EMSResult();
//        result.setObjList(EmployeeStatus.statusList);
//        result.setResultCode(0);
//        result.setMsg("查询成功");
//        return result;
//    }
//
//    @RequestMapping("/getRoleList")
//    public EMSResult getRoleList()
//    {
//        EMSResult result = new EMSResult();
//        List<Role> roleList = roleService.getRoleList(1);
//        result.setObjList(roleList);
//        result.setResultCode(0);
//        result.setMsg("查询成功");
//        return result;
//    }
}
